﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class GiftClass
    {
        private readonly Chain.IDAL.GiftClass dal = new Chain.IDAL.GiftClass();

        public int Add(Chain.Model.GiftClass model)
        {
            if (this.Exists(model.GiftClassName, model.GiftClassID))
            {
                return -1;
            }
            return this.dal.Add(model);
        }

        public void CreateTreeItem(DataTable dtSource, DataTable dt, int ParentClassID, int level)
        {
            if (dt.Columns.Count <= 0)
            {
                for (int j = 0; j < dtSource.Columns.Count; j++)
                {
                    dt.Columns.Add(new DataColumn(dtSource.Columns[j].ColumnName));
                }
            }
            DataRow[] rowArray = dtSource.Select(" GiftParentID=" + ParentClassID);
            for (int i = 0; i < rowArray.Length; i++)
            {
                dt.Rows.Add(dt.NewRow());
                for (int k = 0; k < dt.Columns.Count; k++)
                {
                    string str = rowArray[i][k].ToString();
                    if (dt.Columns[k].ColumnName == "GiftClassName")
                    {
                        str = new string('-', level * 3).ToString() + str;
                    }
                    dt.Rows[dt.Rows.Count - 1][k] = str;
                }
                int parentClassID = Convert.ToInt32(rowArray[i]["GiftClassID"].ToString());
                this.CreateTreeItem(dtSource, dt, parentClassID, level + 1);
            }
        }

        public List<Chain.Model.GiftClass> DataTableToList(DataTable dt)
        {
            List<Chain.Model.GiftClass> list = new List<Chain.Model.GiftClass>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.GiftClass item = new Chain.Model.GiftClass();
                    if ((dt.Rows[i]["GiftClassID"] != null) && (dt.Rows[i]["GiftClassID"].ToString() != ""))
                    {
                        item.GiftClassID = int.Parse(dt.Rows[i]["GiftClassID"].ToString());
                    }
                    if ((dt.Rows[i]["GiftClassName"] != null) && (dt.Rows[i]["GiftClassName"].ToString() != ""))
                    {
                        item.GiftClassName = dt.Rows[i]["GiftClassName"].ToString();
                    }
                    if ((dt.Rows[i]["GiftClassRemark"] != null) && (dt.Rows[i]["GiftClassRemark"].ToString() != ""))
                    {
                        item.GiftClassRemark = dt.Rows[i]["GiftClassRemark"].ToString();
                    }
                    if ((dt.Rows[i]["GiftParentID"] != null) && (dt.Rows[i]["GiftParentID"].ToString() != ""))
                    {
                        item.GiftParentID = int.Parse(dt.Rows[i]["GiftParentID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int GiftClassID)
        {
            return this.dal.Delete(GiftClassID);
        }

        public int DeleteByParentID(int ParentID)
        {
            return this.dal.DeleteByParentID(ParentID);
        }

        public bool DeleteList(string GiftClassIDlist)
        {
            return this.dal.DeleteList(GiftClassIDlist);
        }

        public bool Exists(int GiftClassID)
        {
            return this.dal.Exists(GiftClassID);
        }

        public bool Exists(string ClassName, int classID)
        {
            return this.dal.Exists(ClassName, classID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataTable GetItem(int ClassID)
        {
            return this.dal.GetItem(ClassID).Tables[0];
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.GiftClass GetModel(int GiftClassID)
        {
            return this.dal.GetModel(GiftClassID);
        }

        public List<Chain.Model.GiftClass> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public int Update(Chain.Model.GiftClass model)
        {
            if (this.Exists(model.GiftClassName, model.GiftClassID))
            {
                return -1;
            }
            return this.dal.Update(model);
        }
    }
}

